//
//  AttributeTextViewController.swift
//  funcode
//
//  Created by Wan Jiading on 2017/10/25.
//  Copyright © 2017年 WanJiading. All rights reserved.
//

import UIKit

class AttributeTextViewController: UIViewController {

    @IBOutlet weak var contentLabel: UILabel!
    @IBOutlet weak var resultLabel: UILabel!
    
    @IBOutlet weak var contentTextView: UITextView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        contentLabel.text = "被邀请的好友通过邀请卡进入活动页面并且下单购买会员，您可获得<red>30</red>元无门槛优惠券" // "今年:<red>18</red>身高:<red>180</red>体重:<red>180</red>" //
    }

    @IBAction func changeBtnClick() {
        let attributeText = setTextAttributes(contentLabel.text!)
        resultLabel.attributedText = attributeText
//        contentTextView.attributedText = attributeText
//        do {
//            let lbl = UILabel()
//            self.view.addSubview(lbl)
//            lbl.frame = CGRect(x: 50, y: 200, width: 0, height: 0)
//            lbl.attributedText = attributeText
//            lbl.sizeToFit()
//        } catch (let error) {
//            DebugLog(error)
//        }
        
    }
    
    func setTextAttributes(_ text: String) -> NSAttributedString {
        let contentHighlightStyle: [String : Any] = [
            NSForegroundColorAttributeName  : UIColor.rgb("fa3b3b"),
            NSFontAttributeName             : UIFont.systemFont(ofSize: 14)
        ]
        let attributedText = NSMutableAttributedString(string: text)
        if (attributedText.length > 0) {
            let beginTag = "<red>"
            let endTag = "</red>"
            let regular = "\(beginTag)\\w*\(endTag)"
            let result = try? NSRegularExpression(pattern: regular, options: .init(rawValue: 0))
            let totalRange = NSRange(location: 0, length: attributedText.length)
            let matches = result?.matches(in: text, options: .init(rawValue: 0), range: totalRange)
            if (matches != nil && matches!.count > 0) {
                for match in matches!.reversed() {
                    let matchStr = (text as NSString).substring(with: match.range) as NSString
                    let beginRange = matchStr.range(of: beginTag)
                    let endRange = matchStr.range(of: endTag)
                    let replaceEndRange = NSRange(location: match.range.location + endRange.location,
                                                  length: endRange.length)
                    let replaceBeginRange = NSRange(location: match.range.location + beginRange.location,
                                                    length: beginRange.length)
                    attributedText.replaceCharacters(in: replaceEndRange, with: "")
                    attributedText.replaceCharacters(in: replaceBeginRange, with: "")
                    attributedText.addAttributes(contentHighlightStyle, range: NSRange.init(location: match.range.location, length: match.range.length - beginTag.count - endTag.count))
                }
            }
        }
        return attributedText
    }
}
